﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.Base.Currency;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Item;
    using UFIDA.U9.CBO.SCM.Warehouse;
    using UFIDA.U9.InvDoc.TransferOut;
    using UFIDA.U9.PM.PO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// TransferOutSaveSV partial 
	/// </summary>	
	public partial class TransferOutSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new TransferOutSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class TransferOutSaveSVImpementStrategy : BaseStrategy
	{
		public TransferOutSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			TransferOutSaveSV bpObj = (TransferOutSaveSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

                foreach (var dto in bpObj.Data)
                {
                    if (String.IsNullOrEmpty(dto.DocNo))
                        throw new Exception("必须传入单号[DocNo]");

                    TransferOut doc = TransferOut.Finder.Find($"DocNo='{dto.DocNo}'");
                    if (doc == null)
                    {
                        //新增逻辑
                        doc = TransferOutBase.CreateDoc(dto);
                        //修改单号
                        EntityUtils.UpdateDocNo<TransferOut>(doc.ID, dto.DocNo);
                        //审核
                        //doc = ApproveDoc(doc);
                        //返回值
                        CommonArchiveDataDTOData ret = new CommonArchiveDataDTOData()
                        {
                            ID = doc.ID,
                            Code = doc.DocNo,
                        };
                        retlist.Add(ret);
                    }
                    else
                    {
                        //修改逻辑
                        throw new AITException(599, $"单号:{dto.DocNo}已经存在,不能生成!");
                    }
                }

                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }
    }

	#endregion
	
	
}